package com.goodfox.web.generic.organization.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.organization.entity.Organization;
import com.goodfox.web.generic.organization.mapper.OrganizationMapper;
import com.goodfox.web.generic.organization.service.OrganizationService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;

/**
 * 组织机构Service实现类
 * @author qin gt
 *
 */
@Transactional
@Service(value="organizationService")
@NoteExplain(comment="组织机构管理")
public class OrganizationServiceImpl implements OrganizationService{

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(OrganizationServiceImpl.class);

	/** 组织机构Mapper */
	@Autowired
	private OrganizationMapper organizationMapper;

	@NoteExplain(comment="查询组织机构列表")
	public List<Organization> queryOrganizationListByParentId(String orgIdParent){
		Map map=new HashMap();
		map.put("orgIdParent", orgIdParent);
		return organizationMapper.selectOrganizationList(map);
	}
	
	@NoteExplain(comment="获取组织机构")
	public Organization getOrganization(String orgId){
		return organizationMapper.selectOrganizationById(orgId);
	}

	@NoteExplain(comment="新增组织机构",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addOrganization(Organization entity,String loginLogId){
		entity.setOrgState("1");
		if(entity.getOrgGradeParent()!=null && !entity.getOrgGradeParent().equals("")){
			entity.setOrgGrade(entity.getOrgGradeParent()+1);
		}else{
			entity.setOrgGrade(1L);
		}
		if(entity.getOrgSort()==null){
			Map map=new HashMap();
			map.put("orgIdParent", entity.getOrgIdParent());
			entity.setOrgSort(organizationMapper.selectSubOrganizationListCount(map)+1);
		}
		organizationMapper.insertOrganization(entity);
	}
	
	@NoteExplain(comment="更新组织机构",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyOrganization(Organization oldEny,Organization newEny,String loginLogId){
		newEny.setOrgState("1");
		if(newEny.getOrgGradeParent()!=null && !newEny.getOrgGradeParent().equals("")){
			newEny.setOrgGrade(newEny.getOrgGradeParent()+1);
		}else{
			newEny.setOrgGrade(1L);
		}
		if(newEny.getOrgSort()==null){
			Map map=new HashMap();
			map.put("orgIdParent", newEny.getOrgIdParent());
			newEny.setOrgSort(organizationMapper.selectSubOrganizationListCount(map)+1);
		}
		organizationMapper.updateOrganization(newEny);
	}
	
	@NoteExplain(comment="移除组织机构",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeOrganization(Organization entity,String loginLogId){
		organizationMapper.deleteOrganizationById(entity.getOrgId());
	}
}
